<template>
	<view>
		<!-- 状态栏 -->
		<uniStatusBar bgc="#FFE933"></uniStatusBar>
		<!--  -->
		<view class="login-nav-bg">
			<view class="guanbi icon iconfont icon-guanbi" @tap.stop="close"></view>
			<image src="../../static/nav-status-bg.png" mode="widthFix"></image>
		</view>
		<!-- 登录输入 -->
		<view class="body">
			<block v-if="!yanzhengma">
				<!-- 账号密码登录 -->
				<view class="zhanghao">
					<input type="text" class="uni-input common-input" placeholder="昵称/手机号/邮箱" placeholder-class="placeholder-class"
					 v-model="haoma">
				</view>
				<view class="password">
					<input type="text" password class="uni-input common-input" placeholder="请输入密码" placeholder-class="placeholder-class"
					 v-model="password">
					<view class="u-f u-f-ac">忘记密码？</view>
				</view>
			</block>

			<block v-if="yanzhengma">
				<!-- 验证码登录 -->
				<view class="loginbyshoujihao u-f">
					<view class="quhao u-f u-f-ac">+86</view>
					<input type="text" class="uni-input common-input" placeholder="输入手机号码" placeholder-class="placeholder-class"
					 v-model="phonenum">
				</view>
				<view class="yanzhengma u-f u-f-ac">
					<input type="text" password class="uni-input common-input" placeholder="请输入验证码" placeholder-class="placeholder-class"
					 v-model="checknum">
					<view @tap="getCheckNum">
						<view class="u-f u-f-ac">{{ !checknumcodetime  ? "获取验证码" : checknumcodetime + 's' }}</view>
					</view>
				</view>
			</block>

			<button type="primary" class="user-set-btn" hover-class="exit-hover" :class="{'user-set-btn-disabled': disabled}"
			 :disabled="disabled" @tap="submit">登录</button>
			<!-- 验证码登录 -->
			<view class="loginByyanzhengma u-f u-f-ac" v-if="!yanzhengma">
				<view class="" @tap="changeloginmethod">验证码登录</view>
				<view class="icon iconfont icon-jinru"></view>
			</view>
			<!-- 账号密码登录 -->
			<view class="loginByyanzhengma u-f u-f-ac" v-if="yanzhengma">
				<view class="" @tap="changeloginmethod">账号密码登录</view>
				<view class="icon iconfont icon-jinru"></view>
			</view>

			<!-- 第三方登录 -->
			<view class="otherlogin u-f u-f-ac">
				<view class="line"></view>
				<view>第三方登录</view>
			</view>
			<other-login></other-login>
			<view class="login-agreement u-f u-f-ac">
				<view>注册即代表同意</view>
				<view>《仿糗事百科用户协议》</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniStatusBar from '../../components/uni-status-bar/uni-status-bar.vue'
	import otherLogin from '../../components/home/other-login.vue'

	export default {
		components: {
			uniStatusBar,
			otherLogin,

		},
		data() {
			return {
				haoma: "",
				password: "",
				phonenum: '',
				checknum: "",
				checknumcodetime: 0,
				disabled: true,
				yanzhengma: true
			}
		},
		methods: {
			// 校验手机号合法性
			isPhoneNum() {
				let reg = /^1[3456789]\d{9}$/;
				if (!reg.test(this.phonenum)) {
					uni.showToast({
						title: "手机号码格式错误",
						icon: 'none'
					})
					return false;
				}
					return true;
			},
			// 获取验证码
			getCheckNum() {
				if (this.checknumcodetime > 0) {
					uni.showToast({
						title: "不能重复获取",
						icon: "none"
					})
					return;
				}
				// 验证手机号合法性
				if(this.isPhoneNum()){
					this.checknumcodetime = 10;
					// 向服务器请求验证码
					let timer = setInterval(() => {
						this.checknumcodetime--
						if (this.checknumcodetime < 1) {
							this.checknumcodetime = 0
							clearInterval(timer)
						}
					}, 1000)
				}

				
			},
			// 初始化输入
			initinput() {
				this.haoma = this.password = this.phonenum = this.checknum = ''
			},
			// 改变登录方式
			changeloginmethod() {
				this.initinput()
				this.yanzhengma = !this.yanzhengma
			},
			// 关闭登录页
			close() {
				uni.navigateBack({
					animationType: 'slide-out-bottom'
				})
			},
			// 提交登录
			submit() {
				if (!this.yanzhengma) {
					uni.showToast({
						title: '提交账号密码登录',
						icon: "success"
					})
					return;
				}
				if (!this.isPhoneNum()) {
					uni.showToast({
						title: "请输入正确的手机号码",
						icon: "none"
					})
					return;
				}
				uni.showToast({
					title :"验证码登录成功"
				})
				
				// if(!this.check()){return ;}
				// uni.showToast({
				// 	title:"验证通过",
				// 	icon:"success",
				// 	duration:1500
				// })
			},
			// 验证输入
			check() {
				if ((!this.haoma || this.haoma == "") && (!this.phonenum || this.phonenum == "")) {
					uni.showToast({
						title: "账号不能为空",
						icon: "none"
					})
					return false;
				}
				if ((!this.password || this.password == "") && (!this.checknum || this.checknum == "")) {
					uni.showToast({
						title: "密码不能为空",
						icon: "none"
					})
					return false;
				}
				// if(!this.phonenum || this.phonenum==""){
				// 	uni.showToast({
				// 		title:"手机号不能为空",
				// 		icon:"none"
				// 	})
				// 	return false;
				// }
				// if(!this.checknum || this.checknum==""){
				// 	uni.showToast({
				// 		title:"验证码不能为空",
				// 		icon:"none"
				// 	})
				// 	return false;
				// }
				return true;

			},
			change() {
				if ((this.haoma && this.password) || (this.phonenum && this.checknum)) {
					this.disabled = false
					return;
				}
				this.disabled = true
			}
		},
		watch: {
			haoma(val) {
				this.change()
			},
			password(val) {
				this.change()
			},
			phonenum(val) {
				this.change()
			},
			checknum(val) {
				this.change()
			},
		},
	}
</script>

<style>
	@import "../../common/form.css";

	.login-nav-bg {
		margin-bottom: 40upx;
	}

	.login-nav-bg image {
		width: 100%;
	}

	.placeholder-class {
		font-size: 32upx;
		color: #999999;
		font-weight: bold;
	}

	.login-nav-bg {
		position: relative;
	}

	.guanbi {
		position: absolute;
		top: 0upx;
		left: 20upx;
		font-size: 40upx;
		font-weight: bold;
	}

	.zhanghao {
		position: relative;
	}

	.password,
	.yanzhengma,
	.loginbyshoujihao {
		position: relative;
	}

	.password>input {
		padding-right: 200upx;
	}

	.password>view,
	.yanzhengma>view,
	.loginbyshoujihao .quhao {
		position: absolute;
		height: 100%;
		width: 150upx;
		right: 0;
		top: 0;
	}

	.yanzhengma>view {
		color: #9E9E9E;
		z-index: 10;
		height: auto;
		padding: 10upx;
		font-size: 24upx;
		background-color: #F4F4F4;
		border-radius: 10upx;
		top: auto !important;
		/* width: auto; */
	}

	.loginbyshoujihao .quhao {
		width: 80upx;
		font-size: 35upx;
		font-weight: bold;
		left: 0;
	}

	.user-set-btn {
		margin-top: 40upx;
	}

	.loginbyshoujihao>view {
		width: 100upx;
		height: 100%;

	}

	.loginbyshoujihao>input {
		padding-left: 90upx;
	}

	.otherlogin {
		position: relative;
	}

	.otherlogin>view:first-child {
		width: 50%;
		border-top: 1upx solid #E8E8E8;
		margin: 30upx 0;
	}

	.otherlogin>view:last-child {
		position: absolute;
		color: #E8E8E8;
		top: 6upx;
		padding: 0 20upx;
		background-color: #FFFFFF;
		z-index: 100;
	}

	.loginByyanzhengma {
		padding: 20upx 0;
	}

	.loginByyanzhengma>view:last-child {
		color: #DCDCDC;
	}

	.login-agreement {}

	.login-agreement>view {
		font-size: 24upx;
	}

	.login-agreement>view:first-child {
		color: #E8E8E8;
	}

	.login-agreement>view:last-child {
		color: #49B9FC;
	}
</style>
